<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
    use HasFactory;
//获取用户姓名
public function getCreatedAtAttributes($value)
{
    return $this->diffTime(time()-strtotime($value));
}


//获取用户的电话
    public function user()
    {
        return $this->belongsTo(User::class,'author_id','id')
            ->select("id","nickname","avatar");
    }
    function diffTime($diffTime)
    {
        if ($diffTime < 60){
            return $diffTime.'秒前';
        }elseif ($diffTime > 60 && $diffTime < 60 * 60){
            return ceil(($diffTime/60)).'分钟前';
        }elseif ($diffTime > 60 * 60 && $diffTime < 60 * 60 * 24){
            return ceil(($diffTime/60/24)).'小时前';
        }else{
            return  'emmmm';
        }
    }
    //根据id查数据
  static public function getGoodsDetailByID($id){
    $result=self::with('user')
        ->where('id',$id)
        ->first();
    return $result;

}

}
